<template>
  <basic-container>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-row>
        <el-form-item label="项目编号:">
          <el-input v-model="query.projectCode" clearable placeholder="项目编号" size="small"></el-input>
        </el-form-item>
        <el-form-item label="项目名称:">
          <el-input v-model="query.projectName" clearable placeholder="项目名称"></el-input>
        </el-form-item>
        <el-form-item label="审批状态:">
          <el-select v-model="query.auditStatus" clearable>
            <el-option v-for="dict in dict.type.audit_status" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="tableData" border
              highlight-current-row @cell-click="handleCellClick" style="width: 100%">
      <el-table-column prop="projectCode" label="项目编号"></el-table-column>
      <el-table-column prop="projectName" label="项目名称"></el-table-column>
      <el-table-column prop="submitStatus" label="反馈状态" width="150">
        <template slot-scope="scope">
          <el-tag :type="scope.row.submitStatus | reportAdviceSubmitStatusTag">{{ scope.row.submitStatus | reportAdviceSubmitStatus }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="auditStatus" label="审批状态" width="120">
        <template slot-scope="scope">
          <el-tag :type="scope.row.auditStatus | auditStatusTag">{{ scope.row.auditStatus | auditStatus }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-view" @click="showViewBox(scope.row)">查看</el-button>
          <el-button v-if="permissions.auditAdvice_add && scope.row.auditStatus === 0 && scope.row.submitStatus === 0" type="text" size="small" icon="el-icon-edit" @click="showEditBox(scope.row)">编辑</el-button>
          <el-button v-if="permissions.auditAdvice_reply && (scope.row.submitStatus === 1 || scope.row.submitStatus === 3) && (scope.row.auditStatus === 0 || scope.row.auditStatus === 2)" type="text" size="small" icon="el-icon-edit" @click="showReplyBox(scope.row)">回复意见</el-button>
          <el-button v-if="scope.row.taskId" type="text" size="small" icon="el-icon-edit" @click="showTaskAuditTaskDialog(scope.row)">审批</el-button>
          <el-button v-if="scope.row.auditStatus > 0" type="text" size="small" icon="el-icon-view" @click="showTaskAuditLogDialog(scope.row)">审批日志</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30, 40 ,50 ,100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>

    <el-dialog title="征求意见书-编辑" fullscreen custom-class="ledger" :visible.sync="formBox" :close-on-click-modal="!formBox" :append-to-body="true" :before-close="resetForm" width="1200px" top="5vh">
      <div class="colums">
        <div class="colums_left">
          <div class="colums_left_middle">
            <div class="middleForm ledger">
              <el-form ref="optForm" :model="form" class="el-dialog__body" label-width="100px">
                <div class="ledgerTitle">基本信息</div>
                <div style="margin: 0 auto; width: 945px;">
                  <el-row style="font-size: 15px; line-height: 2.6;">
                    {{ form.auditedCompanyName }}:<br/>
                    &ensp;&ensp;&ensp;&ensp;根据《中华人民共和国审计法》第四十四条的规定，现将对你单位 {{ form.projectName }} 的审计报告
                    送给你们征求意见。请在收到审计报告之日起十日内提出书面意见（写在下面空页内），送交审计处。
                  </el-row>
                  <el-row>
                    <el-col style="display: flex; justify-content: flex-end;">
                      {{form.releaseTime}}
                    </el-col>
                  </el-row>
                </div>
                <div class="title">审计报告</div>
                <div class="inputBox" @click="openWord">
                  <el-input :readonly="true" v-model="form.projectName"></el-input>
                  <div class="inputBoxbg"><i class="el-icon-search"></i></div>
                </div>
<!--                <div class="title">征求意见书</div>-->
<!--                <el-row>-->
<!--                  <el-col>-->
<!--                    <el-form-item label-width="140px" label="征求意见书:">-->
<!--                      <web-document :form-data="form" :readWord="formDisplay?'view':'edit'" pageType="reportAdvice" @createWordFile="wordFileHandle" />-->
<!--                    </el-form-item>-->
<!--                  </el-col>-->
<!--                </el-row>-->
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm" size="small" icon="el-icon-close">关闭</el-button>
                <el-button type="primary" @click="submitAdvice" size="small" icon="el-icon-check" :loading="loading">征求意见</el-button>
              </span>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>

    <el-dialog title="征求意见书-回复" fullscreen custom-class="ledger" :visible.sync="replyBox" :close-on-click-modal="!replyBox" :append-to-body="true" :before-close="resetForm" width="1200px" top="5vh">
      <div class="colums">
        <div class="colums_left">
          <div class="colums_left_middle">
            <div class="middleForm ledger">
              <el-form ref="optForm" :model="form" class="el-dialog__body" label-width="100px">
                <div class="ledgerTitle">基本信息</div>
                <div style="margin: 0 auto; width: 945px;">
                  <el-row style="font-size: 15px; line-height: 2.6;">
                    {{ form.auditedCompanyName }}:<br/>
                    &ensp;&ensp;&ensp;&ensp;根据《中华人民共和国审计法》第四十四条的规定，现将对你单位 {{ form.projectName }} 的审计报告
                    送给你们征求意见。请在收到审计报告之日起十日内提出书面意见（写在下面空页内），送交审计处。
                  </el-row>
                  <el-row>
                    <el-col style="display: flex; justify-content: flex-end;">
                      {{form.releaseTime}}
                    </el-col>
                  </el-row>
                </div>
                <div class="title">审计报告初稿</div>
                <div class="inputBox" @click="openWord">
                  <el-input :readonly="true" v-model="form.projectName"></el-input>
                  <div class="inputBoxbg"><i class="el-icon-search"></i></div>
                </div>
                <div class="title">反馈意见</div>
                <el-row>
                  <el-col>
                    <el-form-item label="反馈意见：">
                      <el-input v-model="form.feedback" type="textarea"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <div class="title">征求意见书</div>
                   <el-row>
                     <el-col>
                       <el-form-item label-width="140px" label="征求意见书:">
                         <web-document :form-data="form" :readWord="formDisplay?'view':'edit'" pageType="reportAdvice" @createWordFile="wordFileHandle" />
                       </el-form-item>
                     </el-col>
                   </el-row>
              </el-form>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm" size="small" icon="el-icon-close">关闭</el-button>
        <el-button type="primary" @click="submit" size="small" icon="el-icon-check" :loading="loading">保存</el-button>
        <el-button type="primary" @click="submitAudit" size="small" icon="el-icon-check" :loading="loading">保存并提交</el-button>
      </span>
    </el-dialog>

    <el-dialog top="5vh" title="征求意见书" :visible.sync="viewBox"
               :append-to-body="true" :before-close="closeViewBox" width="1200px">
      <AuditAdviceView :params="form"></AuditAdviceView>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeViewBox" size="small" icon="el-icon-close">关闭</el-button>
      </span>
    </el-dialog>

    <TaskAuditDialog :dialog="taskAuditDialog" @setValue="setTaskAuditTaskDialog" @closeDialog="closeTaskAuditTaskDialog" ref="taskAuditDialog" />
    <TaskAuditLogDialog :dialog="taskAuditLogDialog" :process-id="processId" @closeDialog="closeTaskAuditTaskLogDialog"/>
  </basic-container>
</template>

<script>
import {mapGetters} from "vuex";
import {fetchList, getAdvice, submit, submitAdvice, submitAudit} from "@/api/adis/report/advice";
import TaskAuditDialog from "@/components/act/TaskAuditDialog";
import TaskAuditLogDialog from "@/components/act/TaskAuditLogDialog";
import AuditAdviceView from "@/components/actForm/AuditAdviceView";
import WebDocument from "@/components/form/webDocument.vue";

export default {
  computed:{
    ...mapGetters(["permissions"])
  },
  components: {WebDocument, AuditAdviceView, TaskAuditDialog, TaskAuditLogDialog},
  dictTypes: ['audit_status'],
  data() {
    return {
      query:{},
      formTitle:'',
      tableData: [],
      form:{
      },
      loading:false,
      formBox:false,
      viewBox: false,
      isView:false,
      page:{
        total: 10,
        currentPage:1
      },
      formDisplay: false,
      replyBox: false,

      taskAuditDialog: false,
      taskAuditLogDialog: false,
      processId: '',
    }
  },
  created() {
    this.onLoad(this.page);
    this.Authorization = 'Bearer ' + this.$store.getters.access_token
  },
  methods:{
    handleCellClick(row, column) {
      if (row && column.label !== "操作"){
        this.showViewBox(row)
      }
    },

    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    onSearch(){
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page)
    },
    onLoad(page, params = {}){
      this.loading = true;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.tableData = data.records;
      }, () => {
        this.loading = false;
      });
    },
    showEditBox(row){
      getAdvice(row.adviceId).then((res)=>{
        this.form = res.data.data;
        this.form.releaseTime = this.form.updateTime.split(" ")[0]
        this.formBox=true;
      })
    },
    resetForm(){
      this.formBox=false;
      this.form = {};
      this.$refs['optForm'] && this.$refs['optForm'].resetFields();
      this.isView =false;
      this.replyBox = false;
    },
    /**
     * 提交征求意见
     */
    submitAdvice(){
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          submitAdvice(this.form).then((res) => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    submit(){
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          submit(this.form).then((res) => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    submitAudit(){
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          submitAudit(this.form).then((res) => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    /**=========审批相关==begin===========*/
    showTaskAuditTaskDialog(row) {
      this.taskAuditDialog = true;
      this.$refs['taskAuditDialog'].initData({taskId: row.taskId, processId: row.processId});
    },
    closeTaskAuditTaskDialog() {
      this.taskAuditDialog = false;
    },
    setTaskAuditTaskDialog() {
      this.taskAuditDialog = false;
      this.onLoad(this.page);
    },
    showTaskAuditLogDialog(row) {
      this.processId = row.processId;
      this.taskAuditLogDialog = true;
    },
    closeTaskAuditTaskLogDialog() {
      this.taskAuditLogDialog = false;
    },
    /**=========审批相关==end===========*/

    showViewBox(row) {
      getAdvice(row.adviceId).then((res) => {
        this.form = res.data.data;
        this.viewBox = true;
        this.isView = true;
      });
    },
    closeViewBox() {
      this.viewBox = false;
    },
    wordFileHandle(e) {
      this.$set(this.form, "wordFile", e.wordFile);
      this.$set(this.form, "adviceId", e.id);
    },

    showReplyBox(row){
      getAdvice(row.adviceId).then((res)=>{
        this.form = res.data.data;
        this.form.releaseTime = this.form.updateTime.split(" ")[0]
        this.replyBox=true;
      })
    },
    downWord() {
      if(!this.form.wordFile) {
        this.$message({
          type: "warning",
          message: "该项尚未上传相关文书!"
        });
        return;
      }
      const a = document.createElement('a');
      let url = this.form.wordFile;
      a.setAttribute('download', '');
      a.setAttribute('href', url);
      a.click();
    },
    openWord() {
      if(!this.form.reportWordFile) {
        this.$message({
          type: "warning",
          message: "该项尚未上传相关文书!"
        });
        return;
      }
      // this.$router.push({path:'/webOffice', query: {url: this.form.wordFile}})
      const { href } = this.$router.resolve({
        path: '/webOffice',
        query: {
          url: this.form.reportWordFile,
          type: 'view',
        }
      });
      window.open(href, '_blank');
    },
  }
}
</script>
